package com.example.service.webhook.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.*;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Configuration
public class DatasourceConfig {
	
	@Autowired
	private Environment env;

	@Value("${email.mysql.url}")
	private String emailUrl;
	@Value("${email.mysql.user}")
	private String emailUser;
	@Value("${email.mysql.pass}")
	private String emailPass;
	
	@Value("${bank.mysql.url}")
	private String bankUrl;
	@Value("${bank.mysql.user}")
	private String bankUser;
	@Value("${bank.mysql.pass}")
	private String bankPass;
	
	@Value("${carrier.mysql.url}")
	private String carrierUrl;
	@Value("${carrier.mysql.user}")
	private String carrierUser;
	@Value("${carrier.mysql.pass}")
	private String carrierPass;
	
	@Value("${chsi.mysql.url}")
	private String chsiUrl;
	@Value("${chsi.mysql.user}")
	private String chsiUser;
	@Value("${chsi.mysql.pass}")
	private String chsiPass;
	
	@Value("${taobao.mysql.url}")
	private String taobaoUrl;
	@Value("${taobao.mysql.user}")
	private String taobaoUser;
	@Value("${taobao.mysql.pass}")
	private String taobaoPass;
	
	@Value("${fund.mysql.url}")
	private String fundUrl;
	@Value("${fund.mysql.user}")
	private String fundUser;
	@Value("${fund.mysql.pass}")
	private String fundPass;
	
	
	@Value("${alipay.mysql.url}")
	private String alipayUrl;
	@Value("${alipay.mysql.user}")
	private String alipayUser;
	@Value("${alipay.mysql.pass}")
	private String alipayPass;
	
	@Value("${jingdong.mysql.url}")
	private String jingdongUrl;
	@Value("${jingdong.mysql.user}")
	private String jingdongUser;
	@Value("${jingdong.mysql.pass}")
	private String jingdongPass;
	
	
	@Value("${qq.mysql.url}")
	private String qqUrl;
	@Value("${qq.mysql.user}")
	private String qqUser;
	@Value("${qq.mysql.pass}")
	private String qqPass;
	
	
	@Value("${zhengxin.mysql.url}")
	private String zhengxinUrl;
	@Value("${zhengxin.mysql.user}")
	private String zhengxinUser;
	@Value("${zhengxin.mysql.pass}")
	private String zhengxinPass;
	
	
	@Value("${insurance.mysql.url}")
	private String insuranceUrl;
	@Value("${insurance.mysql.user}")
	private String insuranceUser;
	@Value("${insurance.mysql.pass}")
	private String insurancePass;

	@Value("${security.mysql.url}")
	private String securityUrl;
	@Value("${security.mysql.user}")
	private String securityUser;
	@Value("${security.mysql.pass}")
	private String securityPass;

    @Value("${court.mysql.url}")
	private String courtUrl;
	@Value("${court.mysql.user}")
	private String courtUser;
	@Value("${court.mysql.pass}")
	private String courtPass;

//	@Value("${extmail.mysql.url}")
//	private String extmailUrl;
//	@Value("${extmail.mysql.user}")
//	private String extmailUser;
//	@Value("${extmail.mysql.pass}")
//	private String extmailPass;
//	
//	@Value("${tenant.mysql.url}")
//	private String tenantUrl;
//	@Value("${tenant.mysql.user}")
//	private String tenantUser;
//	@Value("${tenant.mysql.pass}")
//	private String tenantPass;

	private static final Logger LOGGER = LoggerFactory.getLogger(DatasourceConfig.class);
	private Map<String, Object> datasourceMap;

	@PostConstruct
	public void init() {
		LOGGER.info("打包时间: {}", env.getProperty("package.time"));
		datasourceMap = new HashMap<>();
		for (Iterator<PropertySource<?>> it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext();) {
			PropertySource<?> propertySource = it.next();
			this.getPropertiesFromSource(propertySource, datasourceMap);
		}
	}
	private void getPropertiesFromSource(PropertySource<?> propertySource, Map<String, Object> map) {
		if (propertySource instanceof MapPropertySource) {
			for (String key : ((MapPropertySource) propertySource).getPropertyNames()) {
				map.put(key, propertySource.getProperty(key));
			}
		}
		if (propertySource instanceof CompositePropertySource) {
			for (PropertySource<?> s : ((CompositePropertySource) propertySource).getPropertySources()) {
				getPropertiesFromSource(s, map);
			}
		}
	}

	@Primary
	@Bean(name="templateEmail")
	public JdbcTemplate templateEmail() {
		return new JdbcTemplate(this.getDataSource(emailUrl, emailUser, emailPass));
	}


	@Bean(name="templateBank")
	public JdbcTemplate templateBank() {
		return new JdbcTemplate(this.getDataSource(bankUrl, bankUser, bankPass));
	}
	
	@Bean(name="templateCarrier")
	public JdbcTemplate templateCarrier() {
		return new JdbcTemplate(this.getDataSource(carrierUrl, carrierUser, carrierPass));
	}
	
	
	@Bean(name="templateChsi")
	public JdbcTemplate templateChsi() {
		return new JdbcTemplate(this.getDataSource(chsiUrl, chsiUser, chsiPass));
	}
	
	@Bean(name="templateFund")
	public JdbcTemplate templateFund() {
		return new JdbcTemplate(this.getDataSource(fundUrl, fundUser, fundPass));
	}
	
	
	@Bean(name="templateTaobao")
	public JdbcTemplate templateTaobao() {
		return new JdbcTemplate(this.getDataSource(taobaoUrl, taobaoUser, taobaoPass));
	}
	
	@Bean(name="templateAlipay")
	public JdbcTemplate templateAlipay() {
		return new JdbcTemplate(this.getDataSource(alipayUrl, alipayUser, alipayPass));
	}
	
	@Bean(name="templateJingdong")
	public JdbcTemplate templateJingdong() {
		return new JdbcTemplate(this.getDataSource(jingdongUrl, jingdongUser, jingdongPass));
	}
	
	@Bean(name="templateQq")
	public JdbcTemplate templateQq() {
		return new JdbcTemplate(this.getDataSource(qqUrl, qqUser, qqPass));
	}
	
	@Bean(name="templateZhengxin")
	public JdbcTemplate templateZhengxin() {
		return new JdbcTemplate(this.getDataSource(zhengxinUrl, zhengxinUser, zhengxinPass));
	}
	
	@Bean(name="templateInsurance")
	public JdbcTemplate templateInsurance() {
		return new JdbcTemplate(this.getDataSource(insuranceUrl, insuranceUser, insurancePass));
	}

	@Bean(name="templateSecurity")
	public JdbcTemplate templateSecurity() {
		return new JdbcTemplate(this.getDataSource(securityUrl, securityUser, securityPass));
	}

	@Bean(name="templateCourt")
	public JdbcTemplate templateCourt(){
		return new JdbcTemplate(this.getDataSource(courtUrl, courtUser, courtPass));
	}
//	
//	@Bean(name="templateTenant")
//	public JdbcTemplate templaTetenant() {
//		return new JdbcTemplate(this.getDataSource(tenantUrl, tenantUser, tenantPass));
//	}

	private DataSource getDataSource(String url, String user, String pass) {
		datasourceMap.put(DruidDataSourceFactory.PROP_URL, url);
		datasourceMap.put(DruidDataSourceFactory.PROP_USERNAME, user);
		datasourceMap.put(DruidDataSourceFactory.PROP_PASSWORD, pass);
		try{
			return DruidDataSourceFactory.createDataSource(datasourceMap);
		} catch (Exception e) {
			LOGGER.error("无法获得数据源'{}':'{}'",e);
			throw new RuntimeException("无法获得数据源.");
		}
	}
}
